import EventCenter from "../../lib/EventCenter";
import GameCfgData from "../../lib/GameCfgData";
import Sdk from "../../lib/Sdk";
import { TipManager } from "../../lib/TipManager";
import { ui } from "../../ui/layaMaxUI";
import EventEunm from "../data/EventEunm";
import PlayerData from "../data/PlayerData";
export default class SkinScene extends ui.SkinSceneUI {
    private datalist = [];
    constructor() {
        super();
        this.list.renderHandler = new Laya.Handler(this, this.rederr);
        this.list.mouseHandler = new Laya.Handler(this, this.mouseHandler);
    }
    onEnable() {
        this.datalist = GameCfgData.getIns().data.skin;
        this.list.array = this.datalist;
        this.closeBtn.on(Laya.Event.CLICK, this, this.close);
        Sdk.getIns().showBanner();
    }

    private mouseHandler(e: Laya.Event, index: number) {
        if (e.type == Laya.Event.CLICK) {
            let tcfg = this.datalist[index];
            let tst = this.getSt(tcfg.id);
            if (e.target.name == "btn2") {
                if (tst == 0) {
                    PlayerData.getIns().changeItemType(tcfg.id);
                    this.list.refresh();
                }
            } else if (e.target.name == "btn1") {
                let tisS = tcfg.type  == 0;
                if (tisS) {
                    if (PlayerData.getIns().gold >= tcfg.price) {
                        PlayerData.getIns().unlockItemType(tcfg.id);
                        this.list.refresh();
                        TipManager.ins.showMsgAlert("购买成功", this);
                        PlayerData.getIns().addGold(-tcfg.price);
                        EventCenter.getIns().event(EventEunm.goldChange);
                    } else {
                        TipManager.ins.showMsgAlert("金币不足", this);
                    }
                } else {
                    Sdk.getIns().showAd((_code: number) => {
                        if (_code == 0) {
                            PlayerData.getIns().unlockItemType(tcfg.id);
                            this.list.refresh();
                            TipManager.ins.showMsgAlert("免费购买成功", this);
                        } else {
                            TipManager.ins.showMsgAlert("观看完整视频才能获取奖励", this);
                        }
                    });
                }
            }
        }
    }

    private rederr(_sp: Laya.Sprite, index: number) {
        let tcfg = this.datalist[index];
        // console.log("rederr", tcfg.id);
        let tdata;
        for (let i = 1; i <= 5; i++) {
            tdata = GameCfgData.getIns().getItem(tcfg.id * 5 + i);
            _sp.getChildByName(i + "")["skin"] = tdata.icon;
        }
        let tst = this.getSt(tcfg.id);
        _sp.getChildByName("select")["visible"] = tst == 1;
        _sp.getChildByName("btn2")["visible"] = tst >= 0;
        _sp.getChildByName("btn1")["visible"] = tst == -1;
        if (tst >= 0) {
            _sp.getChildByName("btn2")["skin"] = tst == 0 ? "main/Customization_Green (1).png" : "main/Ghost_bt.png";
            _sp.getChildByName("btn2")["label"] = tst == 0 ? "使用" : "使用中";
        } else {
            let tisS = tcfg.type == 0;
            _sp.getChildByName("btn1")["label"] = tisS ? tcfg.price : "";
            _sp.getChildByName("btn1").getChildByName("icon")["visible"] = tisS;
            _sp.getChildByName("btn1").getChildByName("tip1")["visible"] = !tisS;
            _sp.getChildByName("btn1").getChildByName("tip2")["visible"] = !tisS;
        }

    }

    private getSt(_id: number) {
        let tst = -1;
        if (PlayerData.getIns().isUnlockItemType(_id)) {
            tst = 0;
            if (_id == PlayerData.getIns().currItemType) tst = 1;
        }
        return tst;
    }

    private getLabelBySt(_st: number) {

    }

    onDisable() {
        Sdk.getIns().hideBanner();
    }

}